
import java.sql.*;
import java.util.ArrayList;

public class Item {

  private int id;
  private String nom;
  private Categorie categorie;
  private static final String TABLE_ITEM = "ap4_sp_item";
  private static bdd db = bdd.getInstance();

  // Constructeurs et créateur (dans la BDD)
  public Item(String nom, Categorie categorie) throws SQLException {
    this.nom = nom;
    this.categorie = categorie;
    this.createItem();
  }

  public Item(int idItem, String nom, Categorie categorie) {
    this.id = idItem;
    this.nom = nom;
    this.categorie = categorie;
  }

  private void createItem() throws SQLException {
    try {
      db.update("INSERT INTO " + TABLE_ITEM + "(nom, categorie) VALUES ('" + this.nom + "', " + this.categorie.getId() + ")");
      ResultSet r = db.req("SELECT idItem FROM " + TABLE_ITEM + " WHERE nom='" + this.nom + "'");
      r.next();
      this.id = r.getInt("idItem");
    } catch (SQLException e) {
      throw e;
    }
  }

  // Accesseurs
  public int getId() {
    return this.id;
  }

  public String getNom() {
    return this.nom;
  }

  public Categorie getCategorie() {
    return this.categorie;
  }

  public void setNom(String nom) throws IllegalArgumentException, SQLException {
    if (nom.length() < 50) {
      try {
        db.update("UPDATE " + TABLE_ITEM + " SET nom='" + nom + "' WHERE nom='" + this.nom + "'");
        this.nom = nom;
      } catch (SQLException e) {
        throw e;
      }
    } else {
      throw new IllegalArgumentException("Le nom spécifié est trop long.");
    }
  }

  public void setCategorie(Categorie categorie) throws IllegalArgumentException, SQLException {
    try {
      if (categorie.exists()) {
        this.categorie = categorie;
        db.update("UPDATE " + TABLE_ITEM + " SET categorie=" + categorie.getId() + " WHERE idItem=" + this.getId());
      } else {
        throw new IllegalArgumentException("La catégorie spécifiée n'existe pas !");
      }
    } catch (SQLException e) {
      throw e;
    }
  }

  // Méthodes
  @Override
  public boolean equals(Object o) {
    if (o.getClass() == this.getClass()) {
      if (this.nom.equals(((Item) o).getNom())) {
        if (this.categorie.equals(((Item) o).getCategorie())) {
          return true;
        }
      }
    }

    return false;
  }

  @Override
  public String toString() {
    return "Item " + this.getNom() + "(" + this.categorie.toString() + ")";
  }

  public static ArrayList<Item> chargerItems() throws SQLException {
    ArrayList<Item> items = new ArrayList<Item>();
    ArrayList<Categorie> categories = Categorie.chargerCategories();
    try {
      ResultSet result = db.req("SELECT * FROM " + TABLE_ITEM + " ORDER BY nom ASC");
      while (result.next()) {
        int idItem = result.getInt("idItem");
        String nomItem = result.getString("nom");
        int idCategorie = result.getInt("categorie");
        Categorie foo = new Categorie(idCategorie, Categorie.getCategorie(idCategorie, categories).getNom());
        items.add(new Item(idItem, nomItem, foo));
      }
    } catch (SQLException e) {
      throw e;
    }
    return items;
  }

  public static ArrayList<Item> chargerItem(Categorie categorie) throws SQLException {
    ArrayList<Item> items = Item.chargerItems();
    ArrayList<Item> categorizedItems = new ArrayList<Item>();

    for (Item item : items) {
      if (item.categorie.equals(categorie)) {
        categorizedItems.add(item);
      }
    }

    return categorizedItems;
  }

  public static Item getItem(int id, ArrayList<Item> items) {
    for (Item item : items) {
      if (item.getId() == id) {
        return item;
      }
    }
    return null;
  }

  public static ArrayList<Item> getItemByName(String nom, String ordre) throws SQLException {
    ArrayList<Item> items = new ArrayList<Item>();
    ArrayList<Categorie> categories;
    try {
      categories = Categorie.chargerCategories();
    } catch (SQLException ex) {
      throw ex;
    }
    try {
      ResultSet result = db.req("SELECT * FROM " + TABLE_ITEM + " WHERE nom LIKE('%" + nom + "%') ORDER BY nom " + ordre);
      while (result.next()) {
        int idItem = result.getInt("idItem");
        String nomItem = result.getString("nom");
        int idCategorie = result.getInt("categorie");
        Categorie foo = new Categorie(idCategorie, Categorie.getCategorie(idCategorie, categories).getNom());
        items.add(new Item(idItem, nomItem, foo));
      }
    } catch (SQLException e) {
      throw e;
    }
    return items;
  }

  public int delete() throws SQLException {
    try {
      return db.update("DELETE FROM " + TABLE_ITEM + " WHERE idItem=" + this.id);
    } catch (SQLException e) {
      throw e;
    }
  }

  public static boolean exist(String nom, Categorie categorie) throws SQLException {
    try {
      bdd db = bdd.getInstance();
      ResultSet res = db.req("SELECT * FROM " + TABLE_ITEM + " WHERE nom ='" + nom + "' AND categorie ='" + categorie.getId() + "'");
      return res.first();
    } catch (SQLException e) {
      throw e;
    }
  }
}